import { useModel } from "umi";
import { useReactive } from "ahooks";

const useApplications = () => {
    const { remoteData } = useModel("global");
    const { organizationUuid } = useModel("organization.info");

    const state = useReactive({
        appLoading: false,
        submitLoading: false,
        applications: [],
        totalCount: 0,
    });

    const loadApplicationsByPage = async ({ order, page, pageSize, where }) => {
        state.appLoading = true;
        let res = await remoteData({
            object: "bnt-application-operation-center-service",
            serviceName: "/developer/tools/application/getAll",
            params: {
                appSdud: organizationUuid,
                appStat: "已上架",
                where: where || {},
                limit: pageSize,
                offset: (page - 1) * pageSize || 0,
                order: order || null,
            },
        });
        state.applications = res?.applications || [];
        state.totalCount = res?.totalCount || 0;
        state.appLoading = false;
    };

    return { ...state, loadApplicationsByPage };
};

export default useApplications;
